package dog.weatherwallpaper.utils;

import org.json.JSONObject;

public enum Strings {
	;
	public static boolean isNullOrEmpty(final String s) {
		return s == null || s.trim().length() == 0;
	}

	/**
	 * Tries to convert <b>string</b> to an int.
	 * 
	 * @return converted int or -1 if </b>string</b> cannot be parsed
	 */
	public static int tryParse(final String string) {
		try {
			return Integer.parseInt(string);
		} catch (NumberFormatException e) {
			return -1;
		}
	}

	/**
	 * Returns the value mapped by "value" (if it exists) from the first element of the JSON array.<br>
	 * Returns the empty string if no such mapping exists.
	 * 
	 * @param json
	 *            The JSON object containing a JSON array.
	 * @param arrayName
	 *            the name of the JSON array
	 */
	public static String getValueFromArray(final JSONObject json, final String arrayName) {
		try {
			return json.optJSONArray(arrayName).optJSONObject(0).optString("value");
		} catch (Exception e) {
			App.sendMail(e);
			return "";
		}
	}
}